home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / multiuser / src / support / userinfo.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  9KB  |  328 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Get Information about one or more Users                            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include    <exec/memory.h>
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <dos/datetime.h>
  16. #include <string.h>
  17. #include <libraries/multiuser.h>
  18. #include <proto/multiuser.h>
  19.  
  20. #include "UserInfo_rev.h"
  21.  
  22. #include "Locale.h"
  23.  
  24. char __VersTag__[] = VERSTAG;
  25.  
  26.  
  27. static void __regargs DumpUserInfo(struct muUserInfo *uinfo,
  28.                                               struct muGroupInfo *ginfo, BOOL quick,
  29.                                               BOOL groups, struct muBase *muBase,
  30.                                               struct DosLibrary *DOSBase,
  31.                                               struct ExecBase *SysBase,
  32.                                    struct LocaleInfo *li);
  33.  
  34. int __saveds Start(char *arg)
  35. {
  36.     struct ExecBase *SysBase;
  37.     struct DosLibrary *DOSBase;
  38.     struct muBase *muBase = NULL;
  39.     struct RDArgs *args;
  40.     LONG argarray[] = {
  41. #define argUSERID        0
  42. #define argUID            1
  43. #define argGID            2
  44. #define argNAME        3
  45. #define argGROUPID    4
  46. #define argGROUPNAME    5
  47. #define argALL            6
  48. #define argQUICK        7
  49. #define argGROUPS        8
  50.         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  51.     };
  52.     struct muUserInfo *uinfo;
  53.     struct muGroupInfo *ginfo;
  54.     UWORD gid;
  55.     LONG error = NULL;
  56.     int rc = RETURN_OK;
  57.     struct LocaleInfo li;
  58.  
  59.     SysBase = *(struct ExecBase **)4;
  60.  
  61.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  62.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  63.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  64.         goto Exit;
  65.     }
  66.  
  67.     OpenLoc(&li);
  68.  
  69.     args = ReadArgs("USERID,UID/K/N,GID/K/N,NAME/K,GROUPID/K,GROUPNAME/K,"
  70.                          "ALL/S,Q=QUICK/S,GROUPS/S", argarray, NULL);
  71.     if (!args)
  72.         error = IoErr();
  73.     else
  74.         if (uinfo = muAllocUserInfo()) {
  75.             if (ginfo = muAllocGroupInfo()) {
  76.                 if (!argarray[argALL] && !argarray[argUSERID] && 
  77.                      !argarray[argUID] && !argarray[argGID] && !argarray[argNAME] &&
  78.                      !argarray[argGROUPID] && !argarray[argGROUPNAME]) {
  79.                     if (!argarray[argQUICK])
  80.                         PutStr(GetLocS(&li,MSG_UINFO_THIS));
  81.                     uinfo->uid = muGetTaskOwner(NULL)>>16;
  82.                     if (muGetUserInfo(uinfo, muKeyType_uid))
  83.                         DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  84.                                          argarray[argGROUPS], muBase, DOSBase, SysBase, &li);
  85.                     else
  86.                         PutStr(GetLocS(&li,MSG_NOINFO));
  87.                     goto Done;
  88.                 }
  89.  
  90.                 if (argarray[argALL]) {
  91.                     if (!argarray[argQUICK])
  92.                         PutStr(GetLocS(&li,MSG_UINFO_ALL));
  93.                     if (muGetUserInfo(uinfo, muKeyType_First))
  94.                         do
  95.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  96.                                              argarray[argGROUPS], muBase, DOSBase,
  97.                                              SysBase, &li);
  98.                         while (muGetUserInfo(uinfo, muKeyType_Next) &&
  99.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  100.                     else
  101.                         PutStr(GetLocS(&li,MSG_NOINFO));
  102.                     goto Done;
  103.                 }
  104.  
  105.                 if (argarray[argUSERID]) {
  106.                     if (!argarray[argQUICK])
  107.                         VPrintf(GetLocS(&li,MSG_UINFO_USERID),
  108.                                   &argarray[argUSERID]);
  109.                     strncpy(uinfo->UserID, (char *)argarray[argUSERID],
  110.                               muUSERIDSIZE-1);
  111.                     uinfo->UserID[muUSERIDSIZE-1] = '\0';
  112.                     if (muGetUserInfo(uinfo, muKeyType_WUserID))
  113.                         do
  114.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  115.                                              argarray[argGROUPS], muBase, DOSBase,
  116.                                              SysBase, &li);
  117.                         while (muGetUserInfo(uinfo, muKeyType_WUserIDNext) &&
  118.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  119.                     else
  120.                         PutStr(GetLocS(&li,MSG_NOINFO));
  121.                     goto Done;
  122.                 }
  123.  
  124.                 if (argarray[argUID]) {
  125.                     if (!argarray[argQUICK])
  126.                         VPrintf(GetLocS(&li,MSG_UINFO_UID),
  127.                                   (LONG *)argarray[argUID]);
  128.                     uinfo->uid = (UWORD)*(LONG *)argarray[argUID];
  129.                     if (muGetUserInfo(uinfo, muKeyType_uid))
  130.                         DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  131.                                          argarray[argGROUPS], muBase, DOSBase,
  132.                                      SysBase, &li);
  133.                     else
  134.                         PutStr(GetLocS(&li,MSG_NOINFO));
  135.                     goto Done;
  136.                 }
  137.  
  138.                 if (argarray[argGID] || argarray[argGROUPID] ||
  139.                      argarray[argGROUPNAME]) {
  140.                     if (argarray[argGID]) {
  141.                         if (!argarray[argQUICK])
  142.                             VPrintf(GetLocS(&li,MSG_GINFO_GID),
  143.                                       (LONG *)argarray[argGID]);
  144.                         gid = *(LONG *)argarray[argGID];
  145.                     } else if (argarray[argGROUPID]) {
  146.                         if (!argarray[argQUICK])
  147.                             VPrintf(GetLocS(&li,MSG_GINFO_GROUPID),
  148.                                       &argarray[argGROUPID]);
  149.                         strncpy(ginfo->GroupID, (char *)argarray[argGROUPID],
  150.                                   muGROUPIDSIZE-1);
  151.                         ginfo->GroupID[muGROUPIDSIZE-1] = '\0';
  152.                         if (muGetGroupInfo(ginfo, muKeyType_GroupID))
  153.                             gid = ginfo->gid;
  154.                         else {
  155.                             PutStr(GetLocS(&li,MSG_NOINFO));
  156.                             goto Done;
  157.                         }
  158.                     } else {
  159.                         if (!argarray[argQUICK])
  160.                             VPrintf(GetLocS(&li,MSG_GINFO_GROUPID),
  161.                                       &argarray[argGROUPNAME]);
  162.                         strncpy(ginfo->GroupName, (char *)argarray[argGROUPNAME],
  163.                                   muGROUPNAMESIZE-1);
  164.                         ginfo->GroupID[muGROUPNAMESIZE-1] = '\0';
  165.                         if (muGetGroupInfo(ginfo, muKeyType_GroupName))
  166.                             gid = ginfo->gid;
  167.                         else {
  168.                             PutStr(GetLocS(&li,MSG_NOINFO));
  169.                             goto Done;
  170.                         }
  171.                     }
  172.                     uinfo->gid = gid;
  173.                     if (muGetUserInfo(uinfo, muKeyType_gid))
  174.                         do
  175.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  176.                                              argarray[argGROUPS], muBase, DOSBase,
  177.                                              SysBase, &li);
  178.                         while (muGetUserInfo(uinfo, muKeyType_gidNext) &&
  179.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  180.                     else
  181.                         PutStr(GetLocS(&li,MSG_NOINFO));
  182.                     goto Done;
  183.                 }
  184.  
  185.                 if (argarray[argNAME]) {
  186.                     if (!argarray[argQUICK])
  187.                         VPrintf(GetLocS(&li,MSG_UINFO_USERID),
  188.                                   &argarray[argNAME]);
  189.                     strncpy(uinfo->UserName, (char *)argarray[argNAME],
  190.                               muUSERNAMESIZE-1);
  191.                     uinfo->UserName[muUSERNAMESIZE-1] = '\0';
  192.                     if (muGetUserInfo(uinfo, muKeyType_WUserName))
  193.                         do
  194.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  195.                                              argarray[argGROUPS], muBase, DOSBase,
  196.                                              SysBase, &li);
  197.                         while (muGetUserInfo(uinfo, muKeyType_WUserNameNext) &&
  198.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  199.                     else
  200.                         PutStr(GetLocS(&li,MSG_NOINFO));
  201.                     goto Done;
  202.                 }
  203.  
  204. Done:            muFreeGroupInfo(ginfo);
  205.             } else
  206.                 error = IoErr();
  207.             muFreeUserInfo(uinfo);
  208.         } else
  209.             error = IoErr();
  210.     FreeArgs(args);
  211.     if (error) {
  212.         PrintFault(error, NULL);
  213.         rc = RETURN_ERROR;
  214.     }
  215.  
  216.     CloseLoc(&li);
  217.  
  218. Exit:
  219.     CloseLibrary((struct Library *)muBase);
  220.     CloseLibrary((struct Library *)DOSBase);
  221.  
  222.     return(rc);
  223. }
  224.  
  225.  
  226.     /*
  227.      *        Dump User Information
  228.      */
  229.  
  230. static void __regargs DumpUserInfo(struct muUserInfo *uinfo,
  231.                                               struct muGroupInfo *ginfo, BOOL quick,
  232.                                               BOOL groups, struct muBase *muBase,
  233.                                               struct DosLibrary *DOSBase,
  234.                                               struct ExecBase *SysBase,
  235.                                    struct LocaleInfo *li)
  236. {
  237.     LONG stream[6];
  238.     BPTR dir, file;
  239.     char buffer[256];
  240.     BOOL neverloggedin = TRUE;
  241.     int i;
  242.  
  243.     stream[0] = (LONG)uinfo->UserID;
  244.     stream[1] = uinfo->uid;
  245.     ginfo->gid = uinfo->gid;
  246.     if (muGetGroupInfo(ginfo, muKeyType_gid))
  247.         stream[2] = (LONG)ginfo->GroupID;
  248.     else
  249.         stream[2] = (LONG)"???";
  250.     stream[3] = uinfo->gid;
  251.     stream[4] = (LONG)uinfo->UserName;
  252.     stream[5] = (LONG)uinfo->HomeDir;
  253.  
  254.     if (quick)
  255.         VPrintf("%s\n", stream);
  256.     else {
  257.         VPrintf(GetLocS(li,MSG_UFORMAT_USER), stream);
  258.  
  259.         if (groups)
  260.             if (uinfo->NumSecGroups) {
  261.                 ginfo->gid = uinfo->SecGroups[0];
  262.                 if (muGetGroupInfo(ginfo, muKeyType_gid))
  263.                     stream[0] = (LONG)ginfo->GroupID;
  264.                 else
  265.                     stream[0] = (LONG)"???";
  266.                 stream[1] = uinfo->SecGroups[0];
  267.                 VPrintf(GetLocS(li,MSG_UFORMAT_SECGROUPS), stream);
  268.                 for (i = 1; i < uinfo->NumSecGroups; i++) {
  269.                     ginfo->gid = uinfo->SecGroups[i];
  270.                     if (muGetGroupInfo(ginfo, muKeyType_gid))
  271.                         stream[0] = (LONG)ginfo->GroupID;
  272.                     else
  273.                         stream[0] = (LONG)"???";
  274.                     stream[1] = uinfo->SecGroups[i];
  275.                     VPrintf(GetLocS(li,MSG_UFORMAT_SECGROUPS2), stream);
  276.                 }
  277.                 PutStr("\n");
  278.             } else
  279.                 PutStr(GetLocS(li,MSG_UFORMAT_NOSECGROUPS));
  280.  
  281.         if (dir = Lock(uinfo->HomeDir, SHARED_LOCK)) {
  282.             dir = CurrentDir(dir);
  283.             if (file = Open(muLastLogin_FileName, MODE_OLDFILE)) {
  284.                 if (FGets(file, buffer, 3*LEN_DATSTRING+2))
  285.                     neverloggedin = FALSE;
  286.                 Close(file);
  287.             }
  288.             if (neverloggedin)
  289.                 PutStr(GetLocS(li,MSG_NEVERLOGGED));
  290.             else {
  291.                 stream[0] = (LONG)buffer;
  292.                 stream[1] = (LONG)"";
  293.                 stream[2] = (LONG)"";
  294.                 for (i = 0; buffer[i] && (buffer[i] != ' '); i++);
  295.                 if (buffer[i]) {
  296.                     buffer[i++] = '\0';
  297.                     while (buffer[i] == ' ')
  298.                         i++;
  299.                     stream[1] = (LONG)&buffer[i];
  300.                     while (buffer[i] && (buffer[i] != ' '))
  301.                         i++;
  302.                     if (buffer[i]) {
  303.                         buffer[i++] = '\0';
  304.                         while (buffer[i] == ' ')
  305.                             i++;
  306.                         stream[2] = (LONG)&buffer[i];
  307.                         while (buffer[i] && (buffer[i] != ' ') &&
  308.                                  (buffer[i] != '\n'))
  309.                             i++;
  310.                         buffer[i] = '\0';
  311.                     }
  312.                 }
  313.                 VPrintf(GetLocS(li,MSG_LASTLOGGED), stream);
  314.             }
  315.             if (file = Open(".plan", MODE_OLDFILE)) {
  316.                 PutStr(GetLocS(li,MSG_PLAN));
  317.                 while (!CheckSignal(SIGBREAKF_CTRL_C) &&
  318.                          FGets(file, buffer, 255))            /* V39: use 256 */
  319.                     PutStr(buffer);
  320.                 Close(file);
  321.             } else
  322.                 PutStr(GetLocS(li,MSG_NOPLAN));
  323.             UnLock(CurrentDir(dir));
  324.         } else
  325.             PutStr(GetLocS(li,MSG_NOLOGACCESS));
  326.     }
  327. }
  328.